Jump to content

User:Novem Linguae/sandbox.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// Forked from [[User:Writ Keeper/Scripts/TeahouseTalkbackLink.js]]
// <nowiki>
function tagCategory()
{
	if ($("#mw-content-text").length > 0)
	{
		var $nodeListC = $('a[title^="Category:"]'),
		pdneC = ' (page does not exist)';
		if ($nodeListC !== null)
		{
			var i;
			for (i = 0; i < $nodeListC.length; i++)
			{
				var $link = $($nodeListC[i]);
				var newLinkStringC = "|";
				var redlinkC = false;
				var newLinkHrefC = $link[0].title;
				if(newLinkHrefC.includes(pdneC))
				{
					redlinkC = true;
					newLinkHrefC = newLinkHrefC.replace(pdneC, "");
				}
				var newLinkIdC = "TCsubmit" + i;
				if (redlinkC === false)
				{
				newLinkStringC += '<a style="font-size:x-small; display:inline-block;" title="Tag page for deletion." noPopup=1 id="' + newLinkIdC + 'Delete">Delete</a>|';
				newLinkStringC += '<a style="font-size:x-small; display:inline-block;" title="Tag page as empty category." noPopup=1 id="' + newLinkIdC + 'Tag">Tag</a>';
				$($link).after(newLinkStringC);
				$("#" + newLinkIdC + "Delete").click({"page":newLinkHrefC}, deleteSubmitC);
				$("#" + newLinkIdC + "Tag").click({"page":newLinkHrefC}, tagSubmitC);
				}
			}
		}
	}
}
function checkPageWikitext(page) {
		window.checkingForCategories = 'true';
	    var send_req = {
		    action: 'expandtemplates',
		    text: "{{:"+page+"}}",
		    titles: page,
		    prop: 'wikitext',
		};
		$.get( mw.config.get( 'wgScriptPath' ) + '/api.php', send_req, function( response ) {
		    window.textToCheckForCategories = $( response ).find( '*' ).text();
		    //mw.notify(window.textToCheckForCategories.match('__EXPECTUNUSEDCATEGORY__')+'1')
		    if (window.textToCheckForCategories.match(/__EXPECTUNUSEDCATEGORY__/) != "") {
    			//mw.notify( "Text contains '__EXPECTUNUSEDCATEGORY__', aborted.2")
    			//mw.notify(window.textToCheckForCategories.match(/__EXPECTUNUSEDCATEGORY__/)+'3') // Works
    			window.checkingForCategories = 'false';
    			tagSubmitC2(page);
		    }
	    
});
}
function deleteSubmitC(event) {
	var page = event.data.page;
	window.UnusedCategoriesPage = page
	var params = {
			action: 'query',
			format: 'json',
			titles: page,
			prop: 'categoryinfo'
		},
		api = new mw.Api();
	
	api.get( params ).done( function ( data ) {
		var pages = data.query.pages,
			page;
		for ( page in pages ) {
			window.numPagesInCategoryC = pages[ page ].categoryinfo.pages;
			getPageDataC( page )
		}
	} );
	
}
function getID( page ) {
    mw.notify(page)
	var params = {
		action: 'query',
		format: 'json',
		titles: window.UnusedCategoriesPage,
		prop: 'info',
	},
	api = new mw.Api();

	api.get( params ).done( function ( data ) {
	var pages = data.query.pages,
		p;
	for ( p in pages ) {
		mw.notify(pages[ p ].pageid)
		return pages[ p ].pageid;
	}
} );
}
function getPageDataC( page ) {
	// Get page creator - taken from [[User:Eizen/PageCreator.js]]
	var PageCreator = {
	        getData: function (callback) {
            var that = this;

            this.api.get({
                action: "query",
                prop: "revisions",
                titles: window.UnusedCategoriesPage,
                rvprop: "ids|timestamp|user|userid",
                rvlimit: "1",
                rvdir: "newer",
                format: "json"
            }).done(function ($data) {
                if (!$data.error) {
                    callback(that, $data);
                }
            });
        },
        /**
         * @method handleData
         * @param {object} that
         * @param {json} $result
         * @returns {void}
         */
        handleData: function (that, $result) {
        	//var pageID = getID( page )
        	//mw.notify(pageID)
            var $data = $result.query.pages[page].revisions[0];
            submitDeleteEditsC( $data.user, page );
        },
        init: function () {
        	this.api = new mw.Api();
        	this.getData(this.handleData);
        }
        
	}
	PageCreator.init()
}
function submitDeleteEditsC (pageCreator, page) {
// Check
	if (prompt( "Are you sure? (Category contains "+window.numPagesInCategoryC+" pages.)") === null){
		mw.notify("Aborted.")
		return;
	}
	// Tag for deletion
	var requestDataC =
	{
		/*
		minor: false,
		prependtext: "{{db-catempty|help=off}}\n",
		summary: "Requesting speedy deletion ([[Wikipedia:Criteria for speedy deletion#C1. Unpopulated categories|CSD C1]]) (via [[User:Qwerfjkl/scripts/unusedCategories|script]])"
		*/
	};
	new mw.Api().edit(window.UnusedCategoriesPage, function() {
		return requestDataC;
	}).done(function(data) {
		
			if (data && data.result && data.result === 'Success')
			{
				mw.notify("Page tagged for deletion!");
			}
			else
			{
				alert('There was an error requesting the page edit. Code: ' + data);
			}
	}).fail(function(data) {
			alert('There was an error using AJAX to edit the page. Error: ' + data);
		
	})
	// Notify talk
	var requestDataC2 =
	{
		/*
		minor: false,
		appendtext: "\n{{subst:db-catempty-notice|"+window.UnusedCategoriesPage+"}}",
		summary: "Notifying user ([[Wikipedia:Criteria for speedy deletion#C1. Unpopulated categories|CSD C1]]) (via [[User:Qwerfjkl/scripts/unusedCategories|script]])"
		*/
	};
	mw.notify("Created by: "+pageCreator)
	mw.notify("Tagging: "+window.UnusedCategoriesPage)
	mw.notify("Page ID: "+page)
	new mw.Api().edit('User_talk:'+window.unusedCategoriesPageCreator, function() {
		
		return requestDataC2;
	}).done(function(data) {
		
			if (data && data.result && data.result === 'Success')
			{
				mw.notify("User notified.");
			}
			else
			{
				alert('There was an error requesting the page edit. Code: ' + data);
			}
	}).fail(function(data) {
			alert('There was an error using AJAX to edit the page. Error: ' + data);
		
	})

}
function tagSubmitC(event){
	var page = event.data.page;
	checkPageWikitext(page);

}
function tagSubmitC2(page) {
	if (window.textToCheckForCategories.match(/__EXPECTUNUSEDCATEGORY__/) != null) { // There's a match
    	mw.notify( "Page contains '__EXPECTUNUSEDCATEGORY__', aborted.");
    	//mw.notify(window.textToCheckForCategories.match(/__EXPECTUNUSEDCATEGORY__/));
	    return;
	}
	if (prompt( "Are you sure?") === null){
		mw.notify("Aborted.")
		return;
	}
	var requestDataC =
	{
		/*
		minor: false,
		prependtext: "{{Possibly empty category}}\n",
		summary: "+{{[[Template:Possibly empty category|Possibly empty category]]}} (via [[User:Qwerfjkl/scripts/unusedCategories|script]])"
		*/
	};
	new mw.Api().edit(page, function() {
		return requestDataC;
	}).done(function(data) {
		
			if (data && data.result && data.result === 'Success')
			{
				mw.notify("Page tagged!");
			}
			else
			{
				alert('There was an error requesting the page edit. Code: ' + data);
			}
	}).fail(function(data) {
			alert('There was an error using AJAX to edit the page. Error: ' + data);
	});
}
//if (mw.config.get("wgNamespaceNumber") == -1 && mw.config.get("wgTitle").toLowerCase() == "unusedcategories")
if (mw.config.get("wgPageName") === "Wikipedia:Database_reports/Empty_categories" || mw.config.get("wgPageName") === "Special:UnusedCategories")
{
	mw.loader.using("mediawiki.api", function() 
	{
		// Taken from [[User:Anomie/linkclassifier.js]] to highlight categories tagged for deletion
		importScript('User:Anomie/linkclassifier.js'); // Linkback: [[User:Anomie/linkclassifier.js]]
		importStylesheet('User:Anomie/linkclassifier.css'); // Linkback: [[User:Anomie/linkclassifier.css]]
		tagCategory();
	});
}
// </nowiki>